import createElement from "./createElement.js";
import patchVnode from "./patchVnode.js";
import vnode from "./vnode.js";

export default function patch(oldVode,newVode){
    if(oldVode.sel===undefined || oldVode.sel ===''){
        oldVode = vnode(oldVode.tagName.toLowerCase(),{},[],undefined,oldVode);
    }

    //比较是不是同一个节点
    if(oldVode.key === newVode.key && oldVode.sel === newVode.sel){
       patchVnode(oldVode,newVode)
    }else{
       let newVnodeElm = createElement(newVode);
       if(oldVode.elm.parentNode && newVnodeElm){
          oldVode.elm.parentNode.insertBefore(newVnodeElm,oldVode.elm);
       }
       //删除旧节点
       oldVode.elm.parentNode.removeChild(oldVode.elm);
    }
}